package com.ophiux.utils.ObjConvert;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ophiux.utils.test.StuDto;
import com.ophiux.utils.test.Student;

public class ObjConvertUtil {
	public static void main(String[] args) {

		/*try {
			StuDto dto = new StuDto();
			dto.setAge("22");

			ObjConvertUtil.convert(Student.class, StuDto.class);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		StuDto dto = new StuDto();
		Field[] fields = dto.getClass().getDeclaredFields();
		
		List<Field> fieldList = new ArrayList<Field>();
		fieldList.addAll(Arrays.asList(fields));
		
		Class clz =  dto.getClass().getSuperclass();
		
		//!clz.getName().equals("java.lang.Object")
		while(clz instanceof Object) {
			Field[] sfields = clz.getDeclaredFields();
			fieldList.addAll(Arrays.asList(sfields));
			clz =  clz.getClass().getSuperclass();
		}
		
		for (Field field : fieldList) {
			System.out.println(field.getName());
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
	public static <T> T convert(Class clazz, Class fromClzz) throws Exception {
		// 判断目标类clzz是否为空
		if (clazz == null || fromClzz == null) {
			throw new Exception("target class is null");
		}
		T entity = (T) clazz.newInstance();

		// 获取属性名称数组 ，如 [private java.lang.String com.ophiux.utils.test.StuDto.name,
		// private java.lang.String com.ophiux.utils.test.StuDto.age]
		Field[] fields = fromClzz.getDeclaredFields();
		Field[] newFilds = clazz.getDeclaredFields();
		for (Field field : fields) {
			String name = field.getName();//属性名称，如 name
			String value = getFieldValue(fromClzz ,field.getName()).toString();//获取属性值 
			
			FileKey annotation = field.getAnnotation(FileKey.class);
			ListFileKey listAnnotation = field.getAnnotation(ListFileKey.class);
			if (annotation == null) {
				continue;
			}
			String tagName = annotation.name();// 获取@FileKey(name="NAME")里面的NAME
			String parentTagName = "";
			// if(listAnnotation != null){
			// parentTagName = listAnnotation.name();
			// }else{
			//
			// continue;
			// }

			Class<?> fieldType = field.getType();
			try {
				field.setAccessible(true);
				if (String.class.isAssignableFrom(fieldType)) {
					Field entityField = clazz.getDeclaredField(tagName);
					entityField.setAccessible(true);
					entityField.set(entity, value);
				}
			

			} catch (Exception e) {
				System.out.println("异常了、");
			}

		}

		return null;
	}

	/**
	 * 通过反射，用属性名称获得属性值
	 * @param thisClass  需要获取属性值的类
	 * @param fieldName 该类的属性名称
	 * @return
	 */
	private static Object getFieldValue(Object thisClass, String fieldName) {
		Object value = new Object();
		Method method = null;
		try {
			String methodName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
			method = thisClass.getClass().getMethod("get" + methodName);
			value = method.invoke(thisClass);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
}
